package com.teaRestaurant.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.teaRestaurant.domain.DTO.TablePageQueryDTO;
import com.teaRestaurant.domain.entity.Tables;
import com.teaRestaurant.mapper.TableMapper;
import com.teaRestaurant.service.TableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TableServiceImpl implements TableService {
    @Autowired
    private TableMapper tableMapper;


    @Override
    public List<Tables> list() {
        LambdaQueryWrapper<Tables> wrapper = new LambdaQueryWrapper<>();
        List<Tables> tablesIPage = tableMapper.selectList( wrapper);
        return tablesIPage;
    }

    @Override
    public void insert() {
        LambdaQueryWrapper<Tables> wrapper = new LambdaQueryWrapper<>();
        Long count = tableMapper.selectCount(wrapper);
        Tables table = Tables.builder().status(1).number(0).no(count + 1).build();
        tableMapper.insert(table);
    }

    @Override
    public void delete() {
        // 删除最后一个
        tableMapper.deleteLast();
    }

    @Override
    public void update(List<Tables> tables) {
        for (Tables table : tables) {
            LambdaQueryWrapper<Tables> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Tables::getId, table.getId());
            tableMapper.update(table, wrapper);
        }

    }

    @Override
    public void updateAllStatus() {
        LambdaQueryWrapper<Tables> wrapper = new LambdaQueryWrapper<>();
        List<Tables> tablesList = tableMapper.selectList(wrapper);
        System.out.println(tablesList);
        for (Tables tables : tablesList) {
            if (tables.getStatus() == 2){
                wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(Tables::getId,tables.getId());
                tables.setStatus(1);
                tables.setNumber(0);
                tableMapper.update(tables,wrapper);
            }
        }

    }

    @Override
    public List<Tables> free() {
        LambdaQueryWrapper<Tables> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Tables::getStatus,1);
        List<Tables> tablesList = tableMapper.selectList(wrapper);
        return tablesList;
    }
}
